package com.kaibes.module.file.local.admin;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.core.base.util.FileUtils;
import com.kaibes.core.base.util.StringUtils;
import com.kaibes.module.file.local.FileLocalService;
import com.kaibes.module.file.local.db.FileInfo;
import com.kaibes.module.file.local.db.FileInfoService;
import com.kaibes.module.file.local.db.FileMark;
import com.kaibes.module.file.local.db.FileMarkService;
import com.kaibes.web.admin.AdminController;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.util.ControllerUtils;

@AdminController
@RequestMapping("/admin/file")
public class FileLocalController {
    
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private FileMarkService fileMarkService;
    @Autowired
    private FileLocalService fileLocalService;

    @GetMapping("/info")
    @ApiLink("文件信息管理")
    public String fileInfoManage(Model model, 
            @RequestParam(defaultValue = "25") @Description("每页长度") int size, 
            @RequestParam(defaultValue = "1") @Description("当前页数") int page) {
        model.addAttribute("infoPage", fileInfoService.page(page, size));
        return "file-info";
    }
    
    @GetMapping("/mark")
    @ApiLink("文件标记管理")
    public String fileMarkManage(Model model, 
            @RequestParam(defaultValue = "25") @Description("每页长度") int size,
            @RequestParam(defaultValue = "1") @Description("当前页数") int page, 
            @RequestParam(defaultValue = "0") @Description("搜索内容") int userId,
            @RequestParam(defaultValue = "") @Description("搜索内容") String search) {
        search = search.trim();
        if (!search.isEmpty()) {
            model.addAttribute("markPage", fileMarkService.pageByUuidLikeOrNameLike(page, size, search));
        } else if (userId != 0) {
        	model.addAttribute("markPage", fileMarkService.pageByUserId(page, page, userId));
        } else {
            model.addAttribute("markPage", fileMarkService.page(page, size));
        }
        return "file-mark";
    }
    
    @GetMapping("/mark/self")
    @ApiLink(value = "用户文件管理", validated = false)
    public String userFileMarkManage(Model model, 
    		@RequestAttribute Integer userId,
            @RequestParam(defaultValue = "25") @Description("每页长度") int size,
            @RequestParam(defaultValue = "1") @Description("当前页数") int page, 
            @RequestParam(defaultValue = "") @Description("搜索内容") String search) {
        search = search.trim();
        if (!search.isEmpty()) {
            model.addAttribute("markPage", fileMarkService.pageByUserIdAndUuidLikeOrNameLike(page, size, userId, search));
        } else {
            model.addAttribute("markPage", fileMarkService.pageByUserId(page, size, userId));
        }
        return "file-mark-self";
    }
    
    @ApiLink(value = "查看文件", validated = false)
    @GetMapping("/view/md5/{md5}")
    public String viewMd5(Model model, @PathVariable @Description("文件的MD5") String md5, @RequestParam(required = false, defaultValue = "0") int page) {
    	md5 = StringUtils.removeSuffix(md5);
    	FileInfo info = fileInfoService.getByMD5(md5);
    	if (info == null) {
    		return ControllerUtils.REDIRECT_404;
    	}
    	if (info.getType().startsWith("text")) {
    		String path = info.getPath();
    		model.addAttribute("content", FileUtils.getText(path));
        	return "file-text";
    	} else if (info.getType().startsWith("video")) {
    		model.addAttribute("url", "/files/md5/"+md5);
        	return "file-video";
    	} else if (info.getType().startsWith("image")) {
            model.addAttribute("url", "/files/md5/"+md5);
            model.addAttribute("title", "浏览图片");
        	return "file-image";
    	} else if (info.getType().startsWith("application")) {
    		File[] files = fileLocalService.unZip(info);
    		if (files != null) {
    			model.addAttribute("length", files.length);
    		} else {
    			model.addAttribute("length", 0);
    		}
            model.addAttribute("page", page);
            model.addAttribute("url", "/files/md5/"+md5);
            model.addAttribute("title", "浏览图片");
        	return "file-images";
    	}
    	return ControllerUtils.FORWARD_404;
    }
    
    @ApiLink(value = "查看文件", validated = false)
    @GetMapping("/view/md5/{md5}/{page}")
    public String viewMd5Page(Model model, @PathVariable @Description("文件的MD5") String md5, @PathVariable int page) {
        return viewMd5(model, md5, page);
    }
    
    @ApiLink(value = "查看文件", validated = false)
    @GetMapping("/view/{uuid}")
    public String viewUuid(Model model, @PathVariable @Description("文件的UUID") String uuid, @RequestParam(required = false, defaultValue = "0") int page) {
    	uuid = StringUtils.removeSuffix(uuid);
    	FileMark mark = fileMarkService.getByUuid(uuid);
    	if (mark == null) {
    		return ControllerUtils.REDIRECT_404;
    	}
    	if (mark.getType().startsWith("text")) {
    		FileInfo info = fileInfoService.getById(mark.getFileInfoId());
        	String path = info.getPath();
    		model.addAttribute("content", FileUtils.getText(path));
        	return "file-text";
    	} else if (mark.getType().startsWith("video")) {
    		model.addAttribute("url", "/files/"+uuid);
        	return "file-video";
    	} else if (mark.getType().startsWith("image")) {
            model.addAttribute("url", "/files/"+uuid);
            model.addAttribute("title", "浏览图片");
        	return "file-image";
    	} else if (mark.getType().startsWith("application")) {
    		File[] files = fileLocalService.unZip(mark);
    		if (files != null) {
    			model.addAttribute("length", files.length);
    		} else {
    			model.addAttribute("length", 0);
    		}
            model.addAttribute("page", page);
            model.addAttribute("url", "/files/"+uuid);
            model.addAttribute("title", "浏览图片");
        	return "file-images";
    	}
    	return ControllerUtils.FORWARD_404;
    }
    
    @ApiLink(value = "查看文件", validated = false)
    @GetMapping("/view/{uuid}/{page}")
    public String viewPage(Model model, @PathVariable @Description("文件的UUID") String uuid, 
            @PathVariable int page) {
        return viewUuid(model, uuid, page);
    }
    
}
